package com.wing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wing.constant.MessageConstant;
import com.wing.constant.PasswordConstant;
import com.wing.constant.StatusConstant;
import com.wing.context.BaseContext;
import com.wing.dto.EmployeeDTO;
import com.wing.dto.EmployeeLoginDTO;
import com.wing.dto.EmployeePageQueryDTO;
import com.wing.entity.Employee;
import com.wing.exception.AccountLockedException;
import com.wing.exception.AccountNotFoundException;
import com.wing.exception.PasswordErrorException;
import com.wing.mapper.EmployeeMapper;
import com.wing.result.PageResult;
import com.wing.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //使用DigestUtils来对前端传来的用户密码进行MD5处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //判断当前员工的登录状态
        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 新增员工方法
     *
     * @param employeeDTO
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
//        System.out.println("当前进程的id是:" + Thread.currentThread().getId());

        //将DTO数据转换为employ实体数据
        Employee employee = new Employee();

        //使用对象属性拷贝，前提是属性名必须一致
        //将DTO数据复制到实体类型数据
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置employ的其他属性状态

        //对员工账号状态进行设置
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码，默认密码123456，并对其进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置员工账号的创建时间和修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //设置当前记录的创建人和修改人
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.insert(employee);
    }

    /**
     * 分页查询方法
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {

        //select * from employee limit 0,10-->表示限制查询十条数据实现分页查询
        //开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);


        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 启用禁用员工账号
     * 通过修改employ表中的status字段来实现
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrBan(Integer status, Long id) {

        //创建employee实体对象
        Employee employee = Employee.builder()
                .status(status)
                .id(id).build();

        //update employee set status = ? where id = ?
        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employee getById(Long id) {

        Employee employee = employeeMapper.getById(id);
        //查找到员工信息之后，需要对密码进行处理
        employee.setPassword("****");

        return employee;
    }

    /**
     * 编辑员工信息并保存更新
     * @param employeeDTO
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //进行对象的属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置更新时间
//        employee.setUpdateTime(LocalDateTime.now());
        //设置更新对象
//        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }


}
